package com.atguigu.crowd.service.impl;

import com.atguigu.crowd.entity.Auth;
import com.atguigu.crowd.entity.AuthExample;
import com.atguigu.crowd.mapper.AuthMapper;
import com.atguigu.crowd.service.api.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author zzqstart
 * @Description
 * @date 2022-09-06 22:14
 */
@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    private AuthMapper authMapper;

    @Override
    public List<Auth> getAllAuth() {
        List<Auth> authList = authMapper.selectByExample(new AuthExample());
        return authList;
    }

    @Override
    public List<Integer> getAuthByRoleId(Integer roleId) {
        List<Integer> authIdList = authMapper.selectAuthByRoleId(roleId);
        return authIdList;
    }

    @Override
    public void saveRoleAssignAuth(Map<String, List<Integer>> map) {
        List<Integer> roleIdList = map.get("roleId");
        Integer roleId = roleIdList.get(0);
        List<Integer> authIdArray = map.get("AuthIdArray");

        // 移除roleId所有数据
        authMapper.deleteDataByRoleId(roleId);

        if (authIdArray == null || authIdArray.size() == 0) {
            return;
        }
        // 添加对应数据
        authMapper.insertRoleAssignAuth(roleId, authIdArray);
    }

    @Override
    public void saveAuth(Auth auth) {
        authMapper.insertSelective(auth);
    }

    @Override
    public void editAuth(Auth auth) {
        authMapper.updateByPrimaryKeySelective(auth);
    }

    @Override
    public void removeAuth(Auth auth) {
        authMapper.deleteByPrimaryKey(auth.getId());
    }

    @Override
    public void saveRootAuth(Auth auth) {
        if (auth==null || auth.getTitle()=="" || auth.getTitle()==null){
            throw new RuntimeException("添加失败，不能提交为空");
        }
        authMapper.insertSelective(auth);
    }

    @Override
    public List<String> getAssignedAuthNameByAdminId(Integer adminId) {
        List<String> authNameList = authMapper.getAssignedAuthNameByAdminId(adminId);
        return authNameList;
    }
}
